/**
 * Copyright (c) 2012, lincomb Technology CO,LDT. All rights reserved.
 * Module: App推送
 * File: PushMessageAction.java
 * Purpose: 处理App推送消息Action层
 * Date: 2016-6-6
 */
package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.yunzhi.domain.RequestHeader;
import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultConstant;
import com.yunzhi.domain.ResultDTO;
import com.yunzhi.util.RequestUtil;
import com.yunzhi.util.ResponseUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.PushMessageService;
import com.edais.util.CommonConstant;
import com.edais.util.CommonUtil;
import com.edais.util.StringUtil;

/**
 * ClassName: PushMessageAction 
 * @Description: 处理App推送消息Action类
 * @author ZuoJun
 * @date 2016-6-6 16:09
 */
@Controller
@RequestMapping("/getui")

public class PushMessageAction implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = Logger.getLogger(PushMessageAction.class);
	
	@Autowired
	private PushMessageService pushMessageService;
	
	/**
	 * @Description: 保存手机设备信息
	 * @param request
	 * @param response
	 * @author ZuoJun
	 * @date 2016-6-6 16:10
	 */
	@RequestMapping(value = "/saveMobileInfo")
	public void saveMobileInfo(HttpServletRequest request, HttpServletResponse response){
		
		try {

			if (!RequestUtil.validSign(request)) {
				logger.info("无效签名");
				ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
				return;
			}
			if (!RequestUtil.validToken(request)) {
				logger.info("无效token");
				ResponseUtil.write(response, ResultConstant.INVALID_TOKEN);
				return;
			}

			// 解密请求参数
			RequestHeader header = RequestUtil.decodeHeader(request);
			logger.info("header：" + header.toString());
			JSONObject params = RequestUtil.decodeDataJson(request);
			logger.info("data：" + params);
				
			//手机设备编号
			String cid = params.getString("cid");
			
			if(StringUtil.isEmpty(cid)){
				/**
				 * 暂时更改为Consts.SUCCESS_CODE,解决前端提示CID不能为空!
				 */
				ResponseUtil.write(response,new ResultDTO(ResultCode.SUCCESS,"CID不能为空!"));
				return;
			}
			
			//手机设备类型
			String type = params.getString("type");
			
			if(StringUtil.isEmpty(type)){
				
				/**
				 * 暂时更改为Consts.SUCCESS_CODE,解决前端提示出错!
				 */
				ResponseUtil.write(response,new ResultDTO(ResultCode.SUCCESS,"type不能为空!"));
				return;
			}
			
			type = type.toLowerCase();
			if("android".equals(type)){
				params.put("deviceType", "2");
			}else if("ios".equals(type)){
				params.put("deviceType", "1");
			}
			
			ResultDTO result = pushMessageService.saveMobileInfo(params,header);
			try {
				//把之前手机解绑
				pushMessageService.unbindOtherMobile(params,header);
			}catch (Exception e){
				logger.info("解绑之前登录过的手机异常",e);
			}
			ResponseUtil.write(response,result);
		} catch (Exception e) {
			logger.info("-----------------------------/getui/saveMobileInfo GeTuiAction saveMobileInfo() 出现异常!",e);
			ResponseUtil.write(response,ResultConstant.FAILED);
		}
	
	}
	
	/**
	 * @Description: 解除设备绑定
	 * @param request
	 * @param response
	 * @author ZuoJun
	 * @date 2016-7-15 15:35
	 */
	@RequestMapping(value = "/unbindMobile")
	public void unbindMobile(HttpServletRequest request, HttpServletResponse response){
		
		try {

			if (!RequestUtil.validSign(request)) {
				logger.info("无效签名");
				ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
				return;
			}
			if (!RequestUtil.validToken(request)) {
				logger.info("无效token");
				ResponseUtil.write(response, ResultConstant.INVALID_TOKEN);
				return;
			}
			RequestHeader header = RequestUtil.decodeHeader(request);
			JSONObject params = RequestUtil.decodeDataJson(request);

			//手机设备编号
			String cid = params.getString("cid");
			
			if(StringUtil.isEmpty(cid)){
				ResponseUtil.write(response,new ResultDTO(ResultCode.FAILED,"cid不能为空！"));
				return;
			}
			
			//用户ID
			String userId = header.userId2String();
			if(StringUtil.isEmpty(userId)){
				ResponseUtil.write(response,new ResultDTO(ResultCode.FAILED,"userId不能为空！"));
				return;
			}			
			
			ResultDTO result = pushMessageService.unbindMobile(params,header);
			ResponseUtil.write(response,result);
			
		} catch (Exception e) {
			ResponseUtil.write(response,ResultConstant.FAILED);
			logger.info("-----------------------------/getui/unbindMobile GeTuiAction unbindMobile() 出现异常!",e);
		}
	
	}
	
}
